﻿#ifndef FRICHTEXTEDIT_H
#define FRICHTEXTEDIT_H

#include <QWidget>
#include <QTextCharFormat>
#include <QButtonGroup>

namespace Ui {
class FRichTextEdit;
}

class FRichTextEdit : public QWidget
{
    Q_OBJECT

public:
    explicit FRichTextEdit(QWidget *parent = nullptr);
    ~FRichTextEdit();

    QString toPlainText() const;
    QString toHtml() const;

    void insertImage(const QImage &img);
    void addWidget(QWidget* w);

protected:
    void initView();
    QImage scaledToContentWidth(const QImage &img);

private slots:
    // toolbar actions
    void onFontSizeChanged(const QString &sizeText);
    void onBoldClicked(bool checked);
    void onItalicClicked(bool checked);
    void onUnderlineClicked(bool checked);

    void onAlignLeftClicked();
    void onAlignCenterClicked();
    void onAlignRightClicked();
    void onAlignAutoClicked();

    void onTextRLClicked(bool checked);

    void onSuperScriptClicked(bool checked);
    void onSubScriptClicked(bool checked);

    void onInsertLink();
    void onInsertPicture();
    void onColorClicked();
    void onBgColorClicked();

    // update toolbar state when cursor moves
    void onCursorPositionChanged();

    void applyCharFormatToSelection(const QTextCharFormat &format);
    void applyBlockFormatAlignment(Qt::Alignment align);
    QTextCharFormat currentCharFormat() const;
    QTextBlockFormat currentBlockFormat() const;

    void on_btnCopyAsHtml_clicked();

    void on_teBody_anchorClicked(const QUrl &arg1);

private:
    Ui::FRichTextEdit *ui;
    QButtonGroup *m_alignGroup = nullptr;
};

#endif // FRICHTEXTEDIT_H
